
package com.kanjian.music.entity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
import com.google.gson.reflect.TypeToken;
import com.kanjian.music.KanjianApplication;

public class Live extends BaseCode {

    @SerializedName("is_play")
    public int mIsPlay;

    @SerializedName("activity_name")
    public String mActivityName;

    @SerializedName("activity_pic")
    public String mActivityPic;

    @SerializedName("activity_type")
    public String mActivityType;

    @SerializedName("user_name")
    public String mUserName;

    @SerializedName("user_pic")
    public String mUserPic;

    @SerializedName("activity_time")
    public long mActivityTime;

    @SerializedName("server_time")
    public long mServerTime;

    @SerializedName("uid")
    public int mUserId;
    
    @SerializedName("activity_id")
    public String mLiveId;
    
    @SerializedName("activity_detail")
    public String mLiveDesc;

    public String getDisplayTimeStr() {
        if (mIsPlay == 1) {
            return new SimpleDateFormat("M-dd EEEE mm:ss", Locale.CHINA).format(new Date(
                    mActivityTime * 1000));
        } else {
            long diff = mServerTime - mActivityTime;
            long diffMinutes = diff / (60 * 1000) % 60;
            String time;
            if (diffMinutes > 0) {
                time = diffMinutes + "分钟";
            } else {
                time = "1分钟";
            }

            return "已开播" + time;
        }
    }

    public static ArrayList<Live> getLiveListFromJson(String jsonStr) {
        JSONObject jobj = getJSONObjectFromString(jsonStr);
        if (jobj != null) {
            try {
                return KanjianApplication.GSON.fromJson(jobj.getJSONArray("activity_list")
                        .toString(), new TypeToken<List<Live>>() {
                }.getType());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static Live getLiveFromJson(String jsonStr) {
        return KanjianApplication.GSON.fromJson(jsonStr, Live.class);
    }

}
